package client;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;

import server.Actions;
import server.ActionsHelper;

public class ConcurrentCheck {

	static NamingContext ncRef;

	public static void main(String[] args) {

		Properties props = new Properties();

		props.put("org.omg.CORBA.ORBInitialPort", "1050");
		props.put("org.omg.CORBA.ORBInitialHost", "SPOKANE");


		ORB orb = ORB.init(args, props);
		// get the root naming context
		org.omg.CORBA.Object objRef;
		try {
			objRef = orb.resolve_initial_references("NameService");
			ncRef = NamingContextHelper.narrow(objRef);
		} catch (InvalidName e) {
			e.printStackTrace();
		}

		startThreads("SPVM", "SPVM1234");
		startThreads("SPVM", "SPVM5555");
		startThreads("SPL", "SPL1234");
		startThreads("SPVM", "SPVM6666");
		startThreads("SPVM", "SPVM2343");
		startThreads("SPL", "SPL0958");
		startThreads("SPVM", "SPVM5876");
		startThreads("SPVM", "SPVM2222");
		startThreads("SPL", "SPL1244");
		startThreads("SPVM", "SPVM6435");
		startThreads("SPVM", "SPVM7563");
		startThreads("SPL", "SPL5111");

	}

	public static void startThreads(String station, String officerId) {
		Thread thr = new Thread(new RunConcurrent(station, officerId));
		thr.start();
	}

	private static class RunConcurrent implements Runnable {

		String officerId;
		String station;
		LogActionsClient la;
		Actions actions=null;
		String transferStation="";


		public RunConcurrent(String station, String officerId) {
			this.station=station;
			this.officerId=officerId;
			la = new LogActionsClient(station);
			if (station.equals("SPVM"))
				transferStation = "SPB";
			else if (station.equals("SPL"))
				transferStation = "SPVM";
			else if (station.equals("SPB"))
				transferStation = "SPL";
		}

		public void run() {

			// resolve the Object Reference in Naming
			NameComponent nc = new NameComponent(station, "");
			NameComponent path[] = {nc};


			try {
				actions = ActionsHelper.narrow(ncRef.resolve(path));
			} catch (NotFound e1) {
				e1.printStackTrace();
			} catch (CannotProceed e1) {
				e1.printStackTrace();
			} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e1) {
				e1.printStackTrace();
			}

			for (int i=0;i<100;++i) {
				InstanceGenerator igm = new InstanceGenerator('M');
				Date date = null;
				try {
					date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(igm.getDate());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				String response=actions.createMRecord(igm.getFname(), igm.getLname(), igm.getAddress(), date.getTime(), igm.getLocation(), igm.getStatus(), officerId);
				la.addAction(new Date().toString()+" Officer "+officerId+" generated information for new missing person record:\nName:"+igm.getFname()+" "
						+igm.getLname()+" \nAddress:"+igm.getAddress()+" \nDate:"+date+" \nLocation:"+igm.getLocation()+" \nStatus:"+igm.getStatus()
						+"\nServer response: "+response);
				System.out.println(response+" at "+station+" by "+officerId);
				if (i%3==0) {
					System.out.println(actions.transferRecord(response.substring(7, 14), transferStation, officerId)+" transfer initiated by "+officerId);				}
			}


			for (int i=0;i<100;++i) {
				InstanceGenerator igc = new InstanceGenerator('C');
				Date date = null;
				try {
					date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(igc.getDate());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				String response=actions.createCRecord(igc.getFname(), igc.getLname(), igc.getAddress(), date.getTime(), igc.getLocation(), igc.getStatus(), officerId);
				la.addAction(new Date().toString()+" Officer "+officerId+" generated information for new criminal record:\nName:"+igc.getFname()+" "
						+igc.getLname()+" \nAddress:"+igc.getAddress()+" \nDate:"+date+" \nLocation:"+igc.getLocation()+" \nStatus:"+igc.getStatus()
						+"\nServer response: "+response);
				System.out.println(response+" at "+station+" by "+officerId);
				if (i%4==0) {
					System.out.println(actions.transferRecord(response.substring(7, 14), transferStation, officerId)+" transfer initiated by "+officerId);
				}

			}

			/*for (int i=0;i<100;++i) {
				InstanceGenerator igc = new InstanceGenerator('C');
				Date date = null;
				try {
					date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(igc.getDate());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				String response=actions.createCRecord(igc.getFname(), igc.getLname(), igc.getAddress(), date.getTime(), igc.getLocation(), igc.getStatus(), officerId);
				la.addAction(new Date().toString()+" Officer "+officerId+" generated information for new criminal record:\nName:"+igc.getFname()+" "
						+igc.getLname()+" \nAddress:"+igc.getAddress()+" \nDate:"+date+" \nLocation:"+igc.getLocation()+" \nStatus:"+igc.getStatus()
						+"\nServer response: "+response);
				System.out.println(response);

			}*/
		}
	}

}
